import { defineStore } from 'pinia'
import { useDetailState } from '../interface/index'
import { reqGoodDetailInfo,reqLikes,reqHotGoodList,reqLivelys } from '@/api/detail/index'
import type{GoodDetailInfo} from '@/api/detail/interface'
export const useDetailStore = defineStore('detail', {
    state(): useDetailState {
        return {
            goodDetailInfo: null,
            defaultPicUrl:'',
            likes:[],
            hotGoodList:[],
            livelys:[]
        }
    },
    actions: {
        async getGoodDetailInfo(id: number) {
            const res:GoodDetailInfo = await reqGoodDetailInfo(id)
            this.goodDetailInfo = res
            this.defaultPicUrl = res.itemDetail.picUrl1
            const formData = new FormData();
            formData.append('categoryId',res.categoryList.find(category=>category.level === 'L1')!.id+'')
            formData.append('itemId',id+'')
            this.getHotGoodList(formData)
            this.getLivelys()
        },
        async getLikes(id:number){
            let res = await reqLikes(id)
            this.likes = res.items
        },
        async getHotGoodList(data){
            this.hotGoodList = await reqHotGoodList(data)
        },
        async getLivelys(){
            const formData = new FormData();
            formData.append('num','5')
            this.livelys = await reqLivelys(formData)
        }
    },
    getters: {
        itemDetail(state) {
            return state.goodDetailInfo?.itemDetail
        },
        categoryList(state){
            return state.goodDetailInfo?.categoryList
        },
        attrList(state){
            return  state.goodDetailInfo?.attrList
        },
        skuList(state){
            return state.goodDetailInfo?.skuList
        }
    }
})